package spring.boot.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.boot.redis.handler.TestHandler;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate masterStringRedisTemplate;

    @GetMapping("hello")
    public Object hello() {
        masterStringRedisTemplate.opsForValue().set("string", "string11111", 1, TimeUnit.DAYS);


        masterStringRedisTemplate.opsForHash().put("hash", "key1", "value1");
        masterStringRedisTemplate.opsForHash().put("hash", "key2", "value2");
        masterStringRedisTemplate.expire("hash", 1L, TimeUnit.DAYS);

        masterStringRedisTemplate.opsForList().leftPush("list", "list1");
        masterStringRedisTemplate.opsForList().leftPush("list", "list2");
        masterStringRedisTemplate.opsForList().leftPush("list", "list3");
        masterStringRedisTemplate.expire("list", 1L, TimeUnit.DAYS);

        masterStringRedisTemplate.opsForSet().add("set", "1", "2", "3");
        masterStringRedisTemplate.expire("set", 1L, TimeUnit.DAYS);

        masterStringRedisTemplate.opsForZSet().add("zset", "1", 0.1);
        masterStringRedisTemplate.expire("zset", 1L, TimeUnit.DAYS);


        return "hello";
    }

    @GetMapping("send")
    public Object send() {
        masterStringRedisTemplate.convertAndSend(TestHandler.CHANNEL, "hello redis channel");
        return "send";
    }

}
